﻿#pragma once
#include "Lumina2D.h"
#include <iostream>
#include "Squre.h"
#include <random> 

int GetRandomTetrominoType();
Square* RandomNewSquare();
int UpdateFinishedLine(bool if_draw[15][10]);

const int SQURE_SIZE = 30;
const int BEGIN_X = 0;
const int BEGIN_Y = 100;

const int WEIGHT = 10;
const int HEIGHT = 15;

const float FALL_SPEED = 0.02;

const int LAYER_X = 100;
const int LAYER_Y = 200;
class GameScene :public Scene
{
public:
	GameScene(const std::string& name) :Scene(name){}

	void OnCreate()
	{
		score = new TextBox(0, 0, 120, 50, "Your Score: ");
		score->setFillColor(BLACK);
		score->setTextColor(WHITE);

		for (int i = 0; i < HEIGHT; i++)
			for (int j = 0; j < WEIGHT; j++)
				if_draw[i][j] = 0;

		moving_square = RandomNewSquare();

		text_pause = new TextBox(BEGIN_X + WEIGHT * SQUARE_SIZE + 10, BEGIN_Y + 100, 320, 50, "Press Q to pause the game");
		text_pause->setTextColor(WHITE);

		int layer_x = 100;
		int layer_y = 200;
		game_fail_text = new TextBox(layer_x, layer_y, 250, 40, "Your game is failed");
		try_again_button = new Button(layer_x, layer_y + game_fail_text->height, 100, 40, "Try again");
		quit_button = new Button(layer_x + 150, layer_y + game_fail_text->height, 100, 40, "Quit");

	}

	void OnDestroy()
	{
		if (score)
		{
			delete score;
		}

		if (moving_square)
		{
			delete moving_square;
		}

		if (text_pause)
		{
			delete text_pause;
		}

		if (game_fail_text)
		{
			delete game_fail_text;
		}

		if (try_again_button)
		{
			delete try_again_button;
		}

		if (quit_button) 
		{
			delete quit_button;
		}
	}

    void OnActivate()
	{
		OnCreate();
	}
	void OnDeactivate()
	{
		OnDestroy();
	}

	void Update(float delta)
	{
		inputHandler.update();

		if (!if_paused)
		{
			int line = UpdateFinishedLine(if_draw);
			score_count += line * 10;
			moving_square->Move(0, delta * FALL_SPEED);

			if (inputHandler.isKeyDown('A'))
			{
				if (left_move)
				{
					moving_square->Move(-SQUARE_SIZE, 0);
					left_move = false;

				}
				if (moving_square->CheckCollisionLeft(BEGIN_X, BEGIN_Y, BEGIN_X, if_draw))
				{
					moving_square->Move(SQUARE_SIZE, 0);
				}
			}

			if (inputHandler.isKeyDown('D'))
			{
				if (right_move)
				{
					moving_square->Move(SQUARE_SIZE, 0);
					right_move = false;
				}
				if (moving_square->CheckCollisionRight(BEGIN_X, BEGIN_Y, BEGIN_X + WEIGHT * SQUARE_SIZE, if_draw))
				{
					moving_square->Move(-SQUARE_SIZE, 0);
				}
			}

			if (inputHandler.isKeyDown('S'))
			{
				if (down_move)
				{
					moving_square->Move(0, SQUARE_SIZE);
					down_move = false;
				}
			}

			if (inputHandler.isKeyDown('W'))
			{
				if (can_rotate)
				{
					can_rotate = false;
					int temp[4][4];
					moving_square->GetSqure(temp);

					moving_square->Rotate();

					bool left = moving_square->CheckCollisionLeft(BEGIN_X, BEGIN_Y, BEGIN_X, if_draw);
					bool right = moving_square->CheckCollisionRight(BEGIN_X, BEGIN_Y, BEGIN_X + WEIGHT * SQUARE_SIZE, if_draw);
					bool botton = moving_square->CheckCollisionBotton2(BEGIN_X, BEGIN_Y, BEGIN_Y + HEIGHT * SQUARE_SIZE, if_draw);
					if (left || right || botton)
					{
						moving_square->SetSquare(temp);
					}



				}
			}
			if (inputHandler.isKeyUp('A'))
			{
				left_move = true;
			}

			if (inputHandler.isKeyUp('D'))
			{
				right_move = true;
			}

			if (inputHandler.isKeyUp('S'))
			{
				down_move = true;
			}

			if (inputHandler.isKeyUp('W'))
			{
				can_rotate = true;
			}

			if (moving_square->CheckCollisionBotton2(BEGIN_X, BEGIN_Y, BEGIN_Y + HEIGHT * SQUARE_SIZE, if_draw))
			{
				moving_square->Move(0, -SQUARE_SIZE);
				game_end=moving_square->FixSquare(BEGIN_X, BEGIN_Y, if_draw);
				if (!game_end)
				{
					delete moving_square;
					moving_square = RandomNewSquare();
				}
				else

				{
					if_paused = true;
				}
			}
		}
		if (inputHandler.isKeyDown('Q'))
		{
			if (can_pause)
			{
				if (if_paused)
				{
					if_paused = false;
				}
				else
				{
					if_paused = true;
				}

				can_pause = false;
			}
		}

		if (inputHandler.isKeyUp('Q'))
		{
			can_pause = true;
		}

		if (game_end)
		{
			if (inputHandler.isMouseButtonDown(MouseButton::Left))
			{
				int mouse_x = inputHandler.getMouseX();
				int mouse_y = inputHandler.getMouseY();

				if (can_try_again && try_again_button->isClicked(mouse_x, mouse_y))
				{
					can_try_again = false;
					Renew();
				}
				if (can_quit && quit_button->isClicked(mouse_x, mouse_y))
				{
					can_quit = false;
					Renew();
					m_sceneManager->SwitchToScene("start_scene");

				}
			}

			if (inputHandler.isMouseButtonUp(MouseButton::Left))
			{
				can_quit = true;
				can_try_again = true;
			}
		}
	}

	void Render()
	{
		//绘制UI
		score->draw();

		std::wstring wscore_count= std::to_wstring(score_count);
		outtextxy(score->x + score->width+20, score->y+20, wscore_count.c_str());
		//绘制游戏界面
		ShapeRenderer::DrawRect(BEGIN_X, BEGIN_Y, WEIGHT * SQURE_SIZE, HEIGHT * SQURE_SIZE, 0, WHITE,WHITE);
		
		//绘制各个方格
		for (int i = 0; i < HEIGHT; i++)
			for (int j = 0; j < WEIGHT; j++)
				if(if_draw[i][j])
				     ShapeRenderer::DrawRect(BEGIN_X + j * SQURE_SIZE, BEGIN_Y + i * SQURE_SIZE, SQURE_SIZE, SQURE_SIZE, 1, LIGHTGRAY, WHITE);

		//绘制移动方格
		if (moving_square)
		{
			moving_square->Draw();
		}

		if (text_pause)
		{
			text_pause->draw();
		}

		if (game_end)
		{
			ShapeRenderer::DrawRect(LAYER_X, LAYER_Y, game_fail_text->width, game_fail_text->height + try_again_button->height, WHITE, WHITE);
			if (game_fail_text)
			{
				 game_fail_text->draw();
			}

			if (try_again_button)
			{
				try_again_button->draw();
			}

			if (quit_button)
			{
				quit_button->draw();
			}
		}
	}

	void Renew()
	{
		for (int i = 0; i < HEIGHT; i++)
		{
			for (int j = 0; j < WEIGHT; j++)
			{
				if_draw[i][j] = 0;
			}
		}

		moving_square = RandomNewSquare();
		score_count = 0;

		if_paused = false;
		game_end = false;

	}

private:
	InputHandler inputHandler;

	TextBox* score=nullptr;
	bool if_draw[HEIGHT][WEIGHT];
	Square* moving_square = nullptr;

	bool left_move = true;
	bool right_move = true;
	bool down_move = true;
	bool can_rotate = true;

	int score_count = 0;

	bool if_paused=false;
	bool can_pause=true;
	TextBox* text_pause=nullptr;

	bool game_end = false;

	Button* try_again_button=nullptr;
	Button* quit_button = nullptr;
	TextBox* game_fail_text = nullptr;

	bool can_quit = true;
	bool can_try_again = true;
};

